package ru.dirty.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;


public class CommentsListAdapter extends ArrayAdapter<CommentsItem> {
	
	private Activity activity;
    private CommentViewHolder commentViewHolder;
    private String[] imageUrl;
    private CommentsItem commentsItem;
    public final ImageLoader asyncImageLoader;
    
	public CommentsListAdapter(Activity activity,
			List<CommentsItem> commentsItem) {
		super(activity, 0, commentsItem);
		this.activity = activity;
        asyncImageLoader = new ImageLoader(activity);
		// TODO Auto-generated constructor stub
	}
	
	  @Override
	    public View getView(final int position, View convertView, ViewGroup parent) {
		  	
		 // ImageLoader asyncImageLoader = new ImageLoader(activity.getApplicationContext());
	        if (convertView == null) {
	            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = inflater.inflate(R.layout.post_comment_item, null);
	            
	            commentViewHolder = new CommentViewHolder();
	            commentViewHolder.text = (TextView)   convertView.findViewById(R.id.text);
	            commentViewHolder.autor = (TextView)  convertView.findViewById(R.id.autor);
	            commentViewHolder.rating = (TextView) convertView.findViewById(R.id.rating);
	            commentViewHolder.img = (ImageButton)   convertView.findViewById(R.id.imageButton1);

                commentViewHolder.imgClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String imgUrl = getItem((Integer)view.getTag(R.id.img_position)).getImgUrl()[0];
                        if(imgUrl.contains(activity.getString(R.string.youtubeAdressImg)))
                        {
                            String youtubeID = imgUrl.substring(imgUrl.indexOf("/vi/")+4, imgUrl.length()-6);
                            //Log.w("PostSelected",youtubeID);
                            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(activity.getString(R.string.youtubeVideo) + youtubeID)));
                        }
                        else
                        {

                            /*Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
                                         imgUrl));
                                       startActivity(intent);*/

                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.parse("file://" + "/sdcard/dirtyReader/"+imgUrl.hashCode()), "image/*");
                            activity.startActivity(intent);

                        }
                    }
                };
	            convertView.setTag(commentViewHolder);

	        } else {
	        	commentViewHolder = (CommentViewHolder) convertView.getTag();
	        }
	        commentsItem = getItem(position);
	        
	        imageUrl = commentsItem.getImgUrl();
	        
			if(imageUrl==null){
                    commentViewHolder.img.setTag(0);
					commentViewHolder.img.setVisibility(View.GONE);
            }
		    else{
                commentViewHolder.img.setTag(1);
                commentViewHolder.img.setTag(R.id.img_position,position);
		        asyncImageLoader.DisplayImage(imageUrl[0],commentViewHolder.img,true);
		        commentViewHolder.img.setVisibility(View.VISIBLE);
                commentViewHolder.img.setOnClickListener(commentViewHolder.imgClickListener);
		    }

	        commentViewHolder.text.setText(commentsItem.getText());
            commentViewHolder.text.setMovementMethod(LinkMovementMethod.getInstance());
	        commentViewHolder.autor.setText(commentsItem.getAutor());
	        commentViewHolder.rating.setText(commentsItem.getRating());
          Log.w("user http:",commentsItem.getAboutUser());
			
	        return convertView;
	  }

}
